#自定义一个函数，把异常处理信息打印出来
import traceback
def read_file_line(file_path,linenum):
    try:
        fp=open(file_path)
        content=fp.readlines()[linenum]
    except FileNotFoundError:
        print("文件不存在")
        traceback.print_exc()
        return None
    except TypeError as e:
        traceback.print_exc()
        print("出现错误原因",e)
        return None
    except Exception as e:
        traceback.print_exc()
        print("发生未知错误，错误信息是",e)
        return None
    return content
print(read_file_line("/Users/wangxiaoyan60/PycharmProjects/wangxiaoyan-homework/day6/a.txt",0))


# 统计一个目录及子目录下的所有文件名字，不含后缀
import os
def get_all_files_names(dir_path):
    if not os.path.exists(dir_path):
        print("文件夹路径不存在")
        return None
    if not os.path.isdir(dir_path):
        print("不是一个文件夹")
        return None
    file_name=[]
    for root,dirs,files in os.walk(dir_path):
        for file in files:
            file_name.append(os.path.splitext(file)[0])
    return list(set(file_name))
print(get_all_files_names("/Users/wangxiaoyan60/PycharmProjects/wangxiaoyan-homework"))


#计算两个矩阵之和
import copy
def matrix_add(matrix_a,matrix_b):
    if not isinstance(matrix_a,list) or not isinstance(matrix_b,list):
        return None
    if len(matrix_a[0])!=len(matrix_b[0]):
        return None
    result=copy.deepcopy(matrix_a)
    for i in range(len(matrix_a)):
        for j in range(len(matrix_a[i])):
            result[i][j]+=matrix_a[i][j]
    return result

print(matrix_add([[1,2,3],[2,3,4],[5,6,7]],[[1,2,3],[2,3,4],[5,6,7]]))

